package com.winit.openapi.ece.utils;

import java.math.BigDecimal;

import com.alibaba.fastjson.JSONObject;
import com.winit.vms.spi.command.TrackingNoCommand;
import com.winit.vms.spi.vo.ECEEnumConstant;
import com.winit.vms.spi.vo.EceLogisticsInfoVo;

/**
 * 物流信息VO构造器
 * 
 * @version <pre>
 * Author	Version		Date		Changes
 * changjun.xiang 	1.0  		2016年4月11日 	Created
 *
 * </pre>
 * @since 1.
 */
public class EceLogisticInfoBuilder {

    /**
     * 入库物流信息VO构建.
     * 
     * @param json
     * @return
     */
    public static EceLogisticsInfoVo buildInBoundEceLogisticInfo(JSONObject json) {
        EceLogisticsInfoVo vo = new EceLogisticsInfoVo();

        if (json.containsKey("orderNo")) {
            vo.setOrderNo(json.getString("orderNo").trim());
        }
        if (json.containsKey("occurTime")) {
            vo.setOccurTime(json.getDate("occurTime"));
        }
        if (json.containsKey("trackingNo")) {
            vo.setTrackingNo(json.getString("trackingNo"));
        }
        BigDecimal weight = json.getBigDecimal("weight");
        if (null != weight) {
            vo.setRealWeight(weight);
            BigDecimal height = json.getBigDecimal("height");
            BigDecimal width = json.getBigDecimal("width");
            BigDecimal length = json.getBigDecimal("length");
            BigDecimal volume = new BigDecimal(0);

            if (null != length) {
                vo.setRealLength(length);
            }
            if (null != height) {
                vo.setRealHeight(height);
            }
            if (null != width) {
                vo.setRealWidth(width);
            }
            if (null != height && null != width && null != length) {
                volume = height.divide(new BigDecimal(100))
                    .multiply(weight.divide(new BigDecimal(100)))
                    .multiply(length.divide(new BigDecimal(100)));

                vo.setRealVolume(volume);
            }
        }
        vo.setLogisticsStatus(ECEEnumConstant.LogisticsNodeType.IB.getType());

        return vo;
    }

    /**
     * 出库物流信息VO构建.
     * 
     * @param json
     * @return
     */
    public static EceLogisticsInfoVo buildOutBoundEceLogisticInfo(JSONObject json) {
        EceLogisticsInfoVo vo = new EceLogisticsInfoVo();

        if (json.containsKey("orderNo")) {
            vo.setOrderNo(json.getString("orderNo").trim());
        }
        if (json.containsKey("occurTime")) {
            vo.setOccurTime(json.getDate("occurTime"));
        }
        if (json.containsKey("trackingNo")) {
            vo.setTrackingNo(json.getString("trackingNo"));
        }

        vo.setLogisticsStatus(ECEEnumConstant.LogisticsNodeType.OB.getType());

        return vo;
    }

    /**
     * 航班信息VO构建.
     * 
     * @param json
     * @return
     */
    public static EceLogisticsInfoVo buildFlightEceLogisticInfo(JSONObject json) {
        EceLogisticsInfoVo vo = new EceLogisticsInfoVo();

        if (json.containsKey("orderNo")) {
            vo.setOrderNo(json.getString("orderNo").trim());
        }
        if (json.containsKey("onTransferTime")) {
            vo.setOccurTime(json.getDate("onTransferTime"));
        }
        if (json.containsKey("trackingNo")) {
            vo.setTrackingNo(json.getString("trackingNo"));
        }
        if (json.containsKey("airlines")) {
            vo.setAirLines(json.getString("airlines"));
        }
        if (json.containsKey("flight")) {
            vo.setFlight(json.getString("flight"));
        }
        if (json.containsKey("masterWaybillNo")) {
            vo.setMasterBillNo(json.getString("masterWaybillNo"));
        }

        vo.setLogisticsStatus(ECEEnumConstant.LogisticsNodeType.AT.getType());

        return vo;
    }

    /**
     * 转交国内快递物流信息VO构建.
     * 
     * @param json
     * @return
     */
    public static EceLogisticsInfoVo buildTransportExpressEceLogisticInfo(JSONObject json) {
        EceLogisticsInfoVo vo = new EceLogisticsInfoVo();

        if (json.containsKey("orderNo")) {
            vo.setOrderNo(json.getString("orderNo").trim());
        }
        if (json.containsKey("occurTime")) {
            vo.setOccurTime(json.getDate("occurTime"));
        }
        if (json.containsKey("trackingNo")) {
            vo.setTrackingNo(json.getString("trackingNo"));
        }
        if (json.containsKey("expresscompany")) {
            vo.setExpressCompany(json.getString("expresscompany"));
        }

        vo.setLogisticsStatus(ECEEnumConstant.LogisticsNodeType.YT.getType());

        return vo;
    }

    /**
     * TODO 抵达口岸物流信息VO构建.
     * 
     * @param json
     * @return
     */
    public static EceLogisticsInfoVo buildArrivePortEceLogisticInfo(JSONObject json) {
        EceLogisticsInfoVo vo = new EceLogisticsInfoVo();

        if (json.containsKey("orderNo")) {
            vo.setOrderNo(json.getString("orderNo").trim());
        }
        if (json.containsKey("occurTime")) {
            vo.setOccurTime(json.getDate("occurTime"));
        }
        if (json.containsKey("trackingNo")) {
            vo.setTrackingNo(json.getString("trackingNo"));
        }
        // 设置抵达口岸物流节点 ADP
        vo.setLogisticsStatus(ECEEnumConstant.LogisticsNodeType.ADP.getType());
        return vo;
    }

    /**
     * TODO 清关物流信息VO构建.
     * 
     * @param json
     * @return
     */
    public static EceLogisticsInfoVo buildCustomsClearanceEceLogisticInfo(JSONObject json) {
        EceLogisticsInfoVo vo = new EceLogisticsInfoVo();

        if (json.containsKey("orderNo")) {
            vo.setOrderNo(json.getString("orderNo").trim());
        }
        if (json.containsKey("occurTime")) {
            vo.setOccurTime(json.getDate("occurTime"));
        }
        if (json.containsKey("trackingNo")) {
            vo.setTrackingNo(json.getString("trackingNo"));
        }
        // 设置清关 物流节点 DCC
        vo.setLogisticsStatus(ECEEnumConstant.LogisticsNodeType.DCC.getType());
        return vo;
    }

    /**
     * TODO 修改订单跟踪号 OrderCommand构建.
     * 
     * @param json
     * @return
     */
    public static TrackingNoCommand buildUpdateTrackingNoEceLogisticInfo(JSONObject json) {
        TrackingNoCommand command = new TrackingNoCommand();

        if (json.containsKey("orderNo")) {
            command.setOrderNo(json.getString("orderNo").trim());
        }
        if (json.containsKey("trackingNo")) {
            command.setTrackingNo(json.getString("trackingNo"));
        }
        return command;
    }

}
